<?php

/**
 * cart actions.
 *
 * @package    baumarktforsibers.kg
 * @subpackage cart
 * @author     Belitskii Ivan
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class cartActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
    public function executeIndex(sfWebRequest $request)
    {
        $user = $this->getUser();
        if ($user->hasAttribute('orders')) {
            $orders = $user->getAttribute('orders');
            $this->orders = $orders;
        }
        $orders_combined = null;
        if ($user->hasAttribute('orders_combined')) {
            $orders_combined = $user->getAttribute('orders_combined');
        }
        $this->orders_combined = $orders_combined;
        $this->user = $user;
    }
    
    
    /**
     * Method shows to user everything that he ordere before submit order
     * 
     * @param sfWebRequest $request 
     */
    public function executePrecheckout(sfWebRequest $request) 
    {
        // get user
        $user = $this->getUser();
        
        // getting information about boxes
        $lotoks = Doctrine::getTable('products')->findBy('category', 6);
        $lotoks_ids = array();
        foreach ($lotoks as $lot) { $lotoks_ids[] = $lot->getId(); }
        
        if ($user->hasAttribute('orders')) {
            $orders = $user->getAttribute('orders');
            
            // count of boxes
            $big_lotoks = 0;
            $small_lotoks = 0;
            
            // To determine which box should be added it is required to get the weight of the product
            // and the quantity. If the product weight is less than 200 then small box will be added
            // otherwise big box will be added
            foreach ($orders as $order) {
                $productId = $order[3];
                $product = Doctrine::getTable('products')->findOneBy('id', $productId);
                $product_weight = $product->getWeight();
                
                if ($product_weight <= 200 && $product->getCategory()->getId() < 6 && $product->getName() != 'Хлеб') {
                    $small_lotoks += $order[1];
                }
                if ($product_weight > 200 && $product->getCategory()->getId() < 6) {
                    $big_lotoks += $order[1];
                }
            }
            if ($small_lotoks != 0) {
                foreach ($orders as $key => $value) {
                    if ($key == $lotoks_ids[0]) {
                        unset($orders[$key]);
                    }
                }
                $orders[$lotoks_ids[0]] = array("Лоток Маленький", $small_lotoks, $small_lotoks * 3, $lotoks_ids[0], 6);
            }
            if ($big_lotoks != 0) {
                foreach ($orders as $key => $value) {
                    if ($key == $lotoks_ids[1]) {
                        unset($orders[$key]);
                    }
                }
                $orders[$lotoks_ids[1]] = array("Лоток Большой", $big_lotoks, $big_lotoks * 4, $lotoks_ids[1], 6);
            }
            $user->setAttribute('orders', $orders);
            $this->orders = $user->getAttribute('orders');
        }
        if ($user->hasAttribute('orders_combined')) {
            // for combined orders it is always required to put big box
            $combined_orders = $user->getAttribute('orders_combined');
            $lotok_count = 0;
            
            foreach ($combined_orders as $combined_order) {
                if ($combined_order[3] != $lotoks_ids[1] && $combined_order[3] != $lotoks_ids[0]) {
                    $lotok_count += $combined_order[1];
                }
            }
            if ($lotok_count != 0) {
                foreach ($combined_orders as $key => $value) {
                    if ($key == $lotoks_ids[1]) {
                        unset($combined_orders[$key]);
                    }
                }
                $combined_orders[$lotoks_ids[1]] = array("Лоток Большой", $lotok_count, $lotok_count * 4, $lotoks_ids[1], 6);
            }
            $user->setAttribute('orders_combined', $combined_orders);
            $this->orders_combined = $user->getAttribute('orders_combined');
        }
    }
    
    /**
     * 
     * 
     * Method puts information about orders into database
     * 
     * @param sfWebRequest $request 
     */
    public function executeCheckout(sfWebRequest $request) {
        $user = $this->getUser();
        if (!$user->isAuthenticated()) {
            $this->redirect("@homepage");
        }
        
        if ($user->hasAttribute('orders')) {

            $orders = $user->getAttribute('orders');
            foreach ($orders as $order) {
                $product_id = $order[3];
                $quantity = $order[1];
                $total = $order[2];
                $order = new orders();
                $order->setProductId($product_id);
                $order->setUserId($user->getGuarduser()->getId());
                $order->setQuantity($quantity);
                $order->setTotal($total);
                $order->save();
            }

           $user->setAttribute('orders', false);
        }
        if ($user->hasAttribute('orders_combined')) {
            
            $orders = $user->getAttribute('orders_combined');
            foreach ($orders as $order) {
                $product_id = $order[3];                
                $connected_id = $order[4];
                $quantity = $order[1];
                $total = $order[2];
                $order = new orders_combined();
                
                $order->setProductId($product_id);
                $order->setConnectedId($connected_id);
                $order->setUserId($user->getGuarduser()->getId());
                $order->setQuantity($quantity);
                $order->setTotal($total);
                $order->save();
            }
            $user->setAttribute('orders_combined', false);
        }
        $user->setFlash('notice', "Ваш заказ успешно оформлен");
        $this->redirect("@view_order");
    }
}
